<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'proj:safe:pmRiskassessment:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <a-button
          type="default"
          @click="handleConfirm({})"
          v-auth="'proj:safe:pmRiskassessment:edit'"
        >
          <Icon icon="ant-design:check-circle-filled" /> {{ t('确认通过') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectcode }}
        </a>
      </template>
      <template
        v-if="hasPermission(['proj:safe:pmRiskassessment:edit'])"
        #uploadFileFx="{ record }"
      >
        <BasicUpload
          v-model:value="record.dataMap"
          :bizKey="record.id"
          :bizType="'pmRiskassessmentFx_file'"
          :uploadType="'all'"
          :maxNumber="2"
          :maxSize="1"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
          @change="handleUpload($event, record.id)"
        />
      </template>
      <template
        v-if="hasPermission(['proj:safe:pmRiskassessment:edit'])"
        #uploadFileYj="{ record }"
      >
        <BasicUpload
          v-model:value="record.dataMap"
          :bizKey="record.id"
          :bizType="'pmRiskassessmentYj_file'"
          :uploadType="'all'"
          :maxNumber="2"
          :maxSize="1"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
          @change="handleUpload($event, record.id, 'Yj')"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjSafePmRiskassessmentList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, onMounted, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmRiskassessmentDelete,
    pmRiskassessmentListData,
    confirm,
    uploadFile,
  } from '/@/api/proj/safe/pmRiskassessment';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicUpload } from '/@/components/Upload';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const props = defineProps({
    treeCode: String,
  });

  watch(
    () => props.treeCode,
    async () => {
      console.log(props.treeCode,'props.treeCode===');
      await getForm().setFieldsValue({
        'code': props.treeCode,
      });
      reload();
    },
  );

  const { t } = useI18n('proj.safe.pmRiskassessment');
  const { showMessage } = useMessage();
  const { hasPermission } = usePermission();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('安全文件管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('项目编号'),
        field: 'projectcode',
        component: 'Input',
        // defaultValue: userStore.getProjecte.code
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createtime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('通过情况'),
        field: 'ispass',
        component: 'Select',
        componentProps: {
          dictType: 'zf_proj_aqwjtg_status',
        },
      },
      {
        label: t('审批人'),
        field: 'constructionunit',
        component: 'Input',
      },
      // {
      //   label: t('name'),
      //   field: 'name',
      //   component: 'Input',
      // },
      // {
      //   label: t('evaluationtime'),
      //   field: 'evaluationtime',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      // },
      // {
      //   label: t('evaluationresult'),
      //   field: 'evaluationresult',
      //   component: 'Input',
      // },
      // {
      //   label: t('isdelay'),
      //   field: 'isdelay',
      //   component: 'Input',
      // },
      // {
      //   label: t('delaytime'),
      //   field: 'delaytime',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      // },
      // {
      //   label: t('delayfile'),
      //   field: 'delayfile',
      //   component: 'Input',
      // },
      // {
      //   label: t('delayfileupdatetime'),
      //   field: 'delayfileupdatetime',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      // },
      // {
      //   label: t('ispaper'),
      //   field: 'ispaper',
      //   component: 'Input',
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectcode',
      key: 'a.projectcode',
      sorter: true,
      width: 230,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 260,
      align: 'left',
    },
    {
      title: t('项目地址'),
      dataIndex: 'projectInfo.projectaddress',
      key: 'info.projectaddress',
      sorter: true,
      width: 260,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('通过情况'),
      dataIndex: 'ispass',
      key: 'a.ispass',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'zf_proj_aqwjtg_status',
    },
    {
      title: t('审批人'),
      dataIndex: 'constructionunit',
      key: 'a.constructionunit',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('风险评估文件'),
      dataIndex: 'fxpgfile',
      key: 'a.fxpgfile',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'uploadFileFx',
      fixed:'right'
    },
    {
      title: t('应急预案文件'),
      dataIndex: 'yjyafile',
      key: 'a.yjyafile',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'uploadFileYj',
      fixed:'right'
    },
    // {
    //   title: t('name'),
    //   dataIndex: 'name',
    //   key: 'a.name',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('evaluationtime'),
    //   dataIndex: 'evaluationtime',
    //   key: 'a.evaluationtime',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('evaluationresult'),
    //   dataIndex: 'evaluationresult',
    //   key: 'a.evaluationresult',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('isdelay'),
    //   dataIndex: 'isdelay',
    //   key: 'a.isdelay',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('delaytime'),
    //   dataIndex: 'delaytime',
    //   key: 'a.delaytime',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('delayfile'),
    //   dataIndex: 'delayfile',
    //   key: 'a.delayfile',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('delayfileupdatetime'),
    //   dataIndex: 'delayfileupdatetime',
    //   key: 'a.delayfileupdatetime',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('ispaper'),
    //   dataIndex: 'ispaper',
    //   key: 'a.ispaper',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑安全文件'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'proj:safe:pmRiskassessment:edit',
      },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除安全文件'),
      //   popConfirm: {
      //     title: t('是否确认删除安全文件'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'proj:safe:pmRiskassessment:edit',
      // },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: pmRiskassessmentListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmRiskassessmentDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 确认通过
  async function handleConfirm() {
    if (getSelectRows().length) {
      let selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      console.log(selIds.value);
      const res = await confirm({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一行数据！！');
    }
  }

  async function handleUpload(fileList, id, type) {
    const res = await uploadFile({ id, dataMap: fileList, ope: type });
    console.log(res);
    reload();
  }

  function handleSuccess() {
    reload();
  }

  onMounted(async () => {
    handleProjecte()
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectcode:userStore.getProjecte.code
    })
    reload();
    await getForm().setFieldsValue({
      projectcode: userStore.getProjecte.code
    })
  }
</script>
